<?php

class SettingsController extends Zend_Controller_Action
{

	public function indexAction()
	{
		$this->view->pageTitle = 'ADMIN SETTINGS';
	}
	
 	public function updatedbAction() {
 		$this->view->pageTitle = 'Status BIS database';
 		
 		//execute import batchfile
 		exec('D:\import2MySQL.bat', $output);
 		
 		//print output
 		$this->view->console = $output;
 		
 		//update table status in DB
 		$dbcon = Zend_Db_Table::getDefaultAdapter();
 		$this->view->tableStatus = $dbcon->fetchAssoc("SELECT * FROM bis.tbl_tableStatus;");
		
 		//send update information mail
 		try {
 		$mail = new Zend_Mail();
		$mail->setBodyText(implode("\n", $output));
		$mail->setFrom('servermail@localhost', 'BIS Server');
		$mail->addTo(array('felix.kauschmann@activision.com', 'jan.schultner@activision.com', 'leonardo.priore@activision.com', 'sebastian.pohlmeyer@activision.com'));
		$mail->setSubject('BIS has been updated');
		$mail->send();
 		}
 		catch(Exeption $e) {
 			echo $e->getMessage();		
 		}
 	}

 	
  	public function lockfcstAction() {
  		$this->view->pageTitle = 'Lock Sellthrough Forecast';
  		$dbconn = Zend_Db_Table::getDefaultAdapter();
  		try {
  			$timeinfo = $dbconn->fetchPairs('SELECT * FROM bis.tbl_tableStatus WHERE tableName=\'tbl_sellthroughForecastOngoing\' OR tableName=\'tbl_sellthroughForecastMidquarter\'');
  				
  		}  
  		catch ( Exception $e ) {
  			echo $e->getMessage();
  		}
  		$this->view->time = $timeinfo;
  	}
  	
  	public function lockongoingAction() {
  		$dbconn = Zend_Db_Table::getDefaultAdapter();

  		try {
  			$dbconn->query('call bis.lockSellthroughForecastOngoing();');
  			$dbconn->query('UPDATE bis.tbl_tableStatus SET updatetime=CURRENT_TIMESTAMP WHERE bis.tbl_tableStatus.tableName=\'tbl_sellthroughForecastOngoing\'');
  			
  		}
  		catch ( Exception $e ) {
  			echo $e->getMessage();
  		}
  		$this->_helper->json();
  	}
  	
  	public function lockmidquarterAction() {
  		$dbconn = Zend_Db_Table::getDefaultAdapter();
  		
  		try {
  			$dbconn->query('call bis.lockSellthroughForecastMidquarter();');
  			$dbconn->query('UPDATE bis.tbl_tableStatus SET updatetime=CURRENT_TIMESTAMP WHERE bis.tbl_tableStatus.tableName=\'tbl_sellthroughForecastMidquarter\'');
  		}
  		catch ( Exception $e ) {
  			echo $e->getMessage();
  		}
  		$this->_helper->json();
  	}
  	
   	public function updateitemoverviewAction() {
   		
   		$this->view->pageTitle = 'Itemoverview';
   		
   		try {
   			$db = Zend_Db_Table::getDefaultAdapter();
   			$db->query('call updateItemoverview();');
   		}  catch ( Exception $e ) {
  			echo $e->getMessage();
  		}
   		
   	}

}
